package cn.nawang.ebeim.ui.controller;

import cn.nawang.ebeim.client.dto.ClientDto;
import cn.nawang.ebeim.client.process.Process;
import cn.nawang.ebeim.client.process.ProcessType;
import cn.nawang.ebeim.client.service.ClientService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * Created by je on 2016/1/30.
 */
@RestController
@RequestMapping(value = "/api/upload",produces="application/json;charset=UTF-8")
public class UploadController {

    private static final Logger LOG = LoggerFactory.getLogger(DownloadController.class);

    @Resource
    private ClientDto dto ;

    @Resource
    private ClientService clientService ;

    @RequestMapping(value = "/list")
    public String list(){
        List<Process> uploadProcess = dto.getUploadProcess();
        Collections.reverse(uploadProcess);
        return JSON.toJSONString(uploadProcess);
    }

    @RequestMapping(value = "/start/{dsId}")
    public String start(@PathVariable String dsId){
        LOG.info("startUpload:" + dsId);
        clientService.startUploadProcess(dsId);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        return json.toString();
    }

    @RequestMapping(value = "/pause/{dsId}")
    public String pause(@PathVariable String dsId){
        LOG.info("pauseUpload:" + dsId);
        clientService.pauseUploadProcess(dsId);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        return json.toString();
    }

    @RequestMapping(value = "/delete/{dsId}")
    public String delete(@PathVariable String dsId){
        LOG.info("deleteUpload:" + dsId);
        clientService.removeUploadProcess(dsId);
        JSONObject json = new JSONObject();
        json.put("code", "200");
        return json.toString();
    }

}
